﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// From：张帅
/// PS：自走棋战斗结果Item
/// </summary> 
public class ChessResultItem : MonoBehaviour {
    //public GroupSpriteKit frame;
    public TextureKit icon;
    public Transform starRoot;
    public GameObject starObj;
    public Slider slider;
    public Text heroName, aliveRound, demage;

    float startTime;

    public void SetItem(ProtoBuf.Message.AutoChessFightHeroInfo data, double maxDemage) 
	{
        var chessTable = Table.Blo.AutoChessBlo.GetChessTable(data.chessId);
        var heroTable = Table.Blo.TableHeroBlo.GetTableHero(chessTable.heroId);

        icon.TextureName = heroTable.heroPaint;
        heroName.text = heroTable.heroName;
        demage.text = ((int)data.demage).ToString();
        aliveRound.text = data.aliveRoundNum.ToString();
        if (maxDemage <= 0)
        {
            slider.value = 0;
        }
        else
        {
            startTime = Time.time;
            StartCoroutine(PlaySliderEffect((float)(data.demage / maxDemage)));
        }

        starRoot.DestroyChildren();
        for (int i = 0; i < chessTable.star; i++)
        {
            var star = Instantiate<GameObject>(starObj, starRoot);
            star.transform.localScale = Vector3.one;
            star.SetActive(true);
        }
    }

    IEnumerator PlaySliderEffect(float value)
    {
        while(slider.value < value)
        {
            slider.value = Mathf.Lerp(0, value, Time.time - startTime);
            yield return 0;
        }
    }
}
